package com.tfjybj.itoo.teach.provider.controller;


import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.teach.entity.SchoolCalendarEntity;
import com.tfjybj.itoo.teach.model.SchoolCalendarModel;
import com.tfjybj.itoo.teach.provider.service.SchoolCalendarService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * SchoolCalendarController
 * schoolCalendar表
 *
 * @author 田成荣
 * @version 2.0.0
 * @since 2.0.0 2018-11-08 19:34:10
 */
@Api(tags = {"schoolCalendar表接口"})
@RequestMapping(value = "/schoolCalendar")
@RestController
public class SchoolCalendarController {

    @Resource
    private SchoolCalendarService schoolCalendarService;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model SchoolCalendarModel
     * @return 添加的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody SchoolCalendarModel model) {
		if (StringUtils.isEmpty(model.getEventId())){
            return ItooResult.build(ItooResult.FAIL, "事件名称为空");
        }
		if (StringUtils.isEmpty(model.getTermId())){
            return ItooResult.build(ItooResult.FAIL, "学期信息为空");
        }
        if(StringUtils.isEmpty(model.getRoleId())){
		    return  ItooResult.build(ItooResult.FAIL,"角色信息为空");
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        SchoolCalendarEntity schoolCalendarEntity = new SchoolCalendarEntity();
        try {
            String startTime = model.getStartTime().toString();
            model.setStartTime(simpleDateFormat.parse(startTime));
            String endTime = model.getEndTime().toString();
            model.setEndTime(simpleDateFormat.parse(endTime));
        }catch (ParseException e){
            e.printStackTrace();
        }
        model.setId(IdWorker.getIdStr());
        BeanUtils.copyProperties(model,schoolCalendarEntity);
        schoolCalendarService.save(schoolCalendarEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功          
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        schoolCalendarService.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        schoolCalendarService.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model SchoolCalendarModel
     * @return 修改后的结果
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id修改schoolCalendar")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody SchoolCalendarModel model) {
		if (StringUtils.isEmpty(model.getEventId())){
            return ItooResult.build(ItooResult.FAIL, "eventId为空");
        }
		if (StringUtils.isEmpty(model.getTermId())){
            return ItooResult.build(ItooResult.FAIL, "termId为空");
        }
        SchoolCalendarEntity schoolCalendarEntity = new SchoolCalendarEntity();
        BeanUtils.copyProperties(model, schoolCalendarEntity);
        schoolCalendarService.updateById(schoolCalendarEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找SchoolCalendar
     *
     * @param id 主键id
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        SchoolCalendarEntity schoolCalendarEntity = schoolCalendarService.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarEntity);
    }

    /**
     * 分页查询所有SchoolCalendar
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "分页查询所有SchoolCalendar")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<SchoolCalendarEntity> schoolCalendars = schoolCalendarService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendars);
    }
	
	/**
     * 根据事件id模糊查询schoolCalendar
     *
     * @param eventId  事件id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的schoolCalendar
     * @author 田成荣
     * @since 2.0.0 2018-11-08 19:34:10
     */
    @ApiOperation(value = "根据事件id模糊查询schoolCalendar", notes = "分页根据事件id模糊查询schoolCalendar")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页码", dataType = "int", required = true, example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "记录数", dataType = "int", required = true, example = "10")
    })
    @GetMapping(value = "querySchoolCalendarByLikeEventId/{pageNo}/{pageSize}")
    public ItooResult querySchoolCalendarByLikeEventId(@RequestParam(required = false, defaultValue = "") String eventId,
                                                       @PathVariable Integer pageNo, @PathVariable Integer pageSize) {
        Map<String,String> eventIdMap = new HashMap<>();
        eventIdMap.put("考试周","0");
        eventIdMap.put("教学周","1");
        eventIdMap.put("选课周","2");
        eventIdMap.put("评教周","3");
        eventIdMap.put("体测周","4");
        eventIdMap.put("节假日","5");
        eventIdMap.put("军训周","6");
        eventIdMap.put("体检周","7");
        eventIdMap.put("新生报到周","8");
        eventIdMap.put("运动会","9");
        eventId = (eventIdMap.get(eventId));
        PageInfo<SchoolCalendarEntity> schoolCalendarList = schoolCalendarService.queryByLikeEventId(eventId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 下载校历模板，批量导入使用
     *
     * @param response 响应
     * @return 下载模板是否成功
     * @author 刘雅娟
     * @since  2018年11月16日09:58:45
     */
    @ApiOperation(value = "下载校历信息模板，批量导入使用", notes = "下载校历信息模板，批量导入使用")
    @GetMapping(value = {"/downFooTemplate"})
    public ItooResult downFooTemplate(HttpServletResponse response) {
        if (schoolCalendarService.downTemplate(response)) {
            return ItooResult.build(ItooResult.SUCCESS, "下载成功");
        } else {
            return ItooResult.build(ItooResult.FAIL, "下载失败");
        }
    }

    /**
     * 批量导入校历信息
     *
     * @param multipartFile 批量导入的文件
     * @param request       请求
     * @param response      响应
     * @return ItooResult 批量导入的结果
     * @author 刘雅娟
     * @since 2018年11月16日14:23:55
     */
    @ApiOperation(value = "批量导入校历信息", notes = "0001:部分成功；0000:成功；1111:失败")
    @PostMapping(value = {"/importSchoolCalenderByTemplate"})
    public ItooResult importSchoolCalenderByTemplate(@RequestParam(value = "file") MultipartFile multipartFile,
                                                     HttpServletRequest request, HttpServletResponse response) {
        return schoolCalendarService.importTemplate(multipartFile, request, response);
    }

    /**
     * 查询所有的校历信息schoolCalender
     *@param pageNo
     * @param pageSize
     * @author 刘雅娟
     * @since 2.0.0 2018年11月16日19:43:57
     */
    @ApiOperation(value = "查询")
    @GetMapping(value = {"/findSchoolCalender/{pageNo}/{pageSize}"})
    public ItooResult findSchoolCalender(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                         @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<SchoolCalendarEntity> schoolCalendarEntityPageInfo = schoolCalendarService.findSchoolCalender(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarEntityPageInfo);
    }

    /**
     * 根据id查找SchoolCalendar
     *
     * @param schoolYear 年份
     * @author 刘雅娟
     * @since 2.0.0 2018年11月17日19:34:08
     */
    @ApiOperation(value = "根据年份查询校历")
    @GetMapping(value = {"/findByYear/{schoolYear}"})
    public ItooResult findByYear(@ApiParam(value = "年份", required = true) @PathVariable String schoolYear) {
        List<SchoolCalendarEntity> schoolCalendarEntity = schoolCalendarService.findByYear(schoolYear);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarEntity);
    }


    /**
     * 查询近一周的校历信息schoolCalender
     *
     * @author 刘雅娟
     * @since 2.0.0 2018年11月21日08:54:20
     */
    @ApiOperation(value = "查询近一周校历信息")
    @GetMapping(value = {"/findCalenderByDate/"})
    public ItooResult findCalenderByDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,7);
        String currentdate = df.format(new Date());
        String weekdate =df.format(calendar.getTime());

        List<SchoolCalendarEntity> schoolCalendarEntity = schoolCalendarService.findCalenderByDate(currentdate,weekdate);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarEntity);
    }

    @ApiOperation(value = "查询选课时间")
    @GetMapping(value = {"/findCalenderChooseClass/"})
    public ItooResult findCalenderChooseClass() {
        String chooseClass = "2"; //选课周
        List<SchoolCalendarEntity> schoolCalendarEntity = schoolCalendarService.findCalenderChooseClass(chooseClass);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarEntity);
    }

    /**
     * 根据学年事件名称查询具体事件
     * @return
     * @author 刘雅娟
     * @since 2018-11-25 21:18:09
     */
    @ApiOperation(value = "根据学期id和事件名称查询具体事件")
    @GetMapping(value = {"/findCalendEvent/{termId}/{eventId}"})
    public ItooResult findCalendarEvent(@ApiParam(value = "学期", required = true) @PathVariable String termId ,
                                        @ApiParam(value = "事件名称", required = true) @PathVariable String eventId) {
        SchoolCalendarEntity schoolCalendarEntity = schoolCalendarService.findCalendarEvent(termId,eventId);
        if(schoolCalendarEntity != null){
            return ItooResult.build(ItooResult.SUCCESS, "查询成功", schoolCalendarEntity);
        }
        return ItooResult.build(ItooResult.FAIL, "查询数据不存在", schoolCalendarEntity);
    }

    /**
     * 更新校历时间
     * @param startTime
     * @param endTime
     * @return
     * @author 刘雅娟
     * @since 2018-11-29 16:15:16
     */
    @ApiOperation(value = "更新校历事件时间")
    @GetMapping(value = {"/updateCalendar/{eventId}/{termId}/{startTime}/{endTime}"})
    public ItooResult updateCalendar(@ApiParam(value = "事件名称", required = true) @PathVariable String eventId,
                                     @ApiParam(value = "学期Id", required = true) @PathVariable String termId,
                                     @ApiParam(value = "开始时间", required = true) @PathVariable String startTime ,
                                     @ApiParam(value = "结束时间", required = true) @PathVariable String endTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        SchoolCalendarEntity calendarEvent = schoolCalendarService.findCalendarEvent(termId, eventId);
        try {
            calendarEvent.setStartTime(simpleDateFormat.parse(startTime));
            calendarEvent.setEndTime(simpleDateFormat.parse(endTime));
            schoolCalendarService.updateById(calendarEvent);

        }catch (ParseException e){
            e.printStackTrace();
        }
        return ItooResult.build(ItooResult.SUCCESS, "编辑校历成功");
    }

    /**
     * 按条件模糊分页查询校历信息
     * @param pageNo
     * @param pageSize
     * @param termId
     * @param roleId
     * @param schoolYear
     * @return
     * @author 刘雅娟
     * @since 2018年12月3日17:00:39
     */
    @ApiOperation(value = "按条件模糊分页查询校历信息")
    @GetMapping(value = "/queryCalendarByRoleIdListAndGradList/{pageNo}/{pageSize}")
    public ItooResult queryCalendarByRoleIdListAndGradList(
            @ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
            @ApiParam(name = "pageSize", value = "每页条数", required = true, example = "10") @PathVariable Integer pageSize,
            @ApiParam(name = "termId", value = "学期ID") @RequestParam(required = false, defaultValue = "") String termId,
            @ApiParam(name = "roleId", value = "角色ID") @RequestParam(required = false, defaultValue = "") String roleId,
            @ApiParam(name = "schoolYear", value = "年                                                                                                              份") @RequestParam(required = false, defaultValue = "") String schoolYear
    ) {
        PageInfo<SchoolCalendarModel> schoolCalendarModelList = schoolCalendarService.queryCalendarByRoleIdListAndGradList(termId, roleId, schoolYear, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询校历信息成功！", schoolCalendarModelList);
    }
}    
